﻿using Microsoft.Extensions.DependencyInjection;
using Org.BouncyCastle.Asn1.X509;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VIA.Foundation;
using VIA.Foundation.Services;
using VIA.Presentation;
using VIA.Presentation.Services;
using VIA.RbacPassport;
using VIA.SiMoJi.Domain;
using VIA.Toolkit;

namespace VIA.SiMoJi.Presentation
{
    [ViewModel(typeof(IUserPermissionEntryView), IsShared = true)]
    public class UserPermissionEntryViewModel : ViewModel<IUserPermissionEntryView>
    {
        readonly IServiceProvider _service;
        readonly IUserManager _userManager;
        readonly IZKTEcoFace _zktecoFace;
        readonly IDispatcherService _dispatcherService;
        readonly ICommandFactory _commandFactory;

        string _account;
        string _userName;
        string _warningMessage;
        public string Account
        {
            get => _account;
            set => SetProperty(ref _account, value);
        }
        public string UserName
        {
            get => _userName;
            set => SetProperty(ref _userName, value);
        }

        public string WarningMessage
        {
            get => _warningMessage;
            set => SetProperty(ref _warningMessage, value);
        }
        public IRelayCommand RegisterCommand { get; }
        public IRelayCommand QuitCommand { get; }

        public UserPermissionEntryViewModel(IServiceProvider service)
        {
            _service = service;
            _dispatcherService = service.GetService<IDispatcherService>();
            _userManager = service.GetService<IUserManager>();
            _commandFactory = service.GetService<ICommandFactory>();
            _zktecoFace = service.GetService<IZKTEcoFace>();
            _zktecoFace.UploadUserInfoEvent += ZktecoFace_UploadUserInfoEvent;
            RegisterCommand = _commandFactory.Create(() => Register());
        }
        protected override void OnAttach()
        {
            WarningMessage = string.Empty;
            Account = string.Empty;
            UserName = string.Empty;
        }
        void ZktecoFace_UploadUserInfoEvent(object? sender, ZKTEcoFaceUploadUserInfoEvents e)
        {
            _dispatcherService.Invoke(() =>
            {
                Account = e.Request.Account;
                UserName = e.Request.UserName;
            });
        }

        async Task Register()
        {
            try
            {
                var currentUser = _service.GetService<ICurrentUser>();
                if (Account.IsNullOrEmpty())
                {
                    WarningMessage = "账户为空";
                    return;
                }
                if (UserName.IsNullOrEmpty())
                {
                    WarningMessage = "用户名为空";
                    return;
                }
                User user = new User();
                user.Account = Account;
                user.Name = UserName;
                user.EmployeeId = Account;
                user.CardNumber = Account;
                user.Password = SecurityHelper.Md5Hash(Account);
                user.CreateTime = DateTime.Now;
                user.CreatorId = currentUser.User.Id;
                user.LoginMode = UserLoginMode.AccountAndPassword;
                await _userManager.InsertOrUpdate(user);
                WarningMessage = "注册成功";
            }
            catch (Exception ex)
            {
                WarningMessage = $"注册用户失败{ex.Message}";
            }
        }
    }
}
